﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Baidu.Aip.Ocr;

namespace Ftest.answers
{
    public partial class frmMain : System.Windows.Forms.Form
    {
        static Ocr client = new Ocr(gpBaidu.gsApiKey, gpBaidu.gsSecretKey);
        string gsSearchUrl = "https://www.baidu.com/s?wd={0}";

        #region 热键
        private const int WM_HOTKEY = 0x312; //窗口消息：热键
        private const int WM_CREATE = 0x1; //窗口消息：创建
        private const int WM_DESTROY = 0x2; //窗口消息：销毁
        private const int HotKeyID = 2; //热键ID（自定义）
        protected override void WndProc(ref Message msg)
        {
            base.WndProc(ref msg);
            switch (msg.Msg)
            {
                case WM_HOTKEY: //窗口消息：热键
                    int tmpWParam = msg.WParam.ToInt32();
                    if (tmpWParam == HotKeyID)
                    {
                        //System.Windows.Forms.SendKeys.Send("^v");
                        letsroll();
                    }
                    break;
                case WM_CREATE: //窗口消息：创建
                    SystemHotKey.RegHotKey(this.Handle, HotKeyID, SystemHotKey.KeyModifiers.None, Keys.F2);
                    break;
                case WM_DESTROY: //窗口消息：销毁
                    SystemHotKey.UnRegHotKey(this.Handle, HotKeyID); //销毁热键
                    break;
                default:
                    break;
            }
        }
        #endregion

        public frmMain()
        {
            InitializeComponent();
            #region 画矩形
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
            this.InitObjs();
            #endregion
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            #region panel拖动 调整
            for (int i = 0; i < this.panel1.Controls.Count; i++)
            {
                this.panel1.Controls[i].MouseDown += new System.Windows.Forms.MouseEventHandler(MyMouseDown);
                this.panel1.Controls[i].MouseLeave += new System.EventHandler(MyMouseLeave);
                this.panel1.Controls[i].MouseMove += new System.Windows.Forms.MouseEventHandler(MyMouseMove);
                //this.Controls[i].MouseDown += new System.Windows.Forms.MouseEventHandler(MyMouseDown);
                //this.Controls[i].MouseLeave += new System.EventHandler(MyMouseLeave);
                //this.Controls[i].MouseMove += new System.Windows.Forms.MouseEventHandler(MyMouseMove);
            }
            #endregion

            //webBrowser1.ScriptErrorsSuppressed = true;
            letsroll();
        }

        public string GeneralEnhanced()//string imgPath
        {
            //var image = File.ReadAllBytes(imgPath);
            if (pCut.Image == null)
                return "";
            var image = bmp2byte((Bitmap)pCut.Image);
            // 通用文字识别
            var result = client.GeneralBasic(image);
            // 图片url
            //result = client.GeneralBasic("https://www.baidu.com/img/bd_logo1.png");
            // 带生僻字版
            //var result1 = client.GeneralEnhanced(image);

            //return result + "\r\n\r\n=====>>>>" + result1;
            try
            {
                baiduOcr aa = Newtonsoft.Json.JsonConvert.DeserializeObject<baiduOcr>(result.ToString());
                string tmp = "",wordStr="";
                for (int i = 0; i < aa.words_result.Length; i++)
                {
                    tmp = aa.words_result[i].words.Trim();
                    //if (i != 0&&(tmp.StartsWith("1.") || tmp.StartsWith("A.")))
                    //    break;
                    wordStr += tmp;
                }
                return wordStr;
            }
            catch
            {
            }
            return "";
        }

        //Bitmap   =>   byte[] 
        private Byte[] bmp2byte(Bitmap b)
        {
            //Bitmap b = new Bitmap("test.bmp ");
            MemoryStream ms = new MemoryStream();
            b.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            byte[] bytes = ms.GetBuffer();  //byte[]   bytes=   ms.ToArray(); 这两句都可以，至于区别么，下面有解释
            ms.Close();

            return bytes;
        }
        //byte[]    =>   Bitmap
        //private void byte2bmp()
        //{
        //    byte[] bytelist = bytes;
        //    MemoryStream ms1 = new MemoryStream(bytelist);
        //    Bitmap bm = (Bitmap)Image.FromStream(ms1);
        //    ms1.Close(); 
        //}


        private void button1_Click(object sender, EventArgs e)
        {
            //创建图象，保存将来截取的图象
            Bitmap image = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            Graphics imgGraphics = Graphics.FromImage(image);
            //设置截屏区域 柯乐义
            imgGraphics.CopyFromScreen(0, 0, 0, 0, new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));
            //保存
            //string fileName = "D:/1.jpg";
            //SaveImage(fileName,image);
            //System.Diagnostics.Process.Start(fileName);
            //不保存  直接显示
            this.BackgroundImage = image;
            Color cTransparencyKey = this.TransparencyKey;
            this.TransparencyKey = Color.White;
        }
        private void SaveImage(string fileName, Image image)
        {
            //string fileName ="D:/1.jpg";
            string extension = Path.GetExtension(fileName);
            if (extension == ".jpg")
            {
                image.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            else
            {
                image.Save(fileName, System.Drawing.Imaging.ImageFormat.Bmp);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }
        
        private void letsroll()
        {
            //创建图象，保存将来截取的图象
            Bitmap image = new Bitmap//(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            (pCutSet.Width,pCutSet.Height);//画布大小调整
            Graphics imgGraphics = Graphics.FromImage(image);
            //设置截屏区域 柯乐义
            imgGraphics.CopyFromScreen(
                pCutSet.Location.X,
                pCutSet.Location.Y,
                //pCutSet.Location.X + pCutSet.Width,
                0, 0,
                //pCutSet.Location.Y + pCutSet.Height, 
                //new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
                pCutSet.Size
                , CopyPixelOperation.SourceCopy
            );
            this.pCut.Image = image;
            //识别
            string r = GeneralEnhanced();
            int ibeg = r.IndexOf(".");
            if (ibeg > 0&& ibeg<=2)
                r = r.Substring(ibeg + 1);
            r = r.Replace("A.", " ").Replace("B.", " ").Replace("C.", " ").Replace("D.", " ")
                .Replace("0%", " ").Replace("%0", " ");
            edtOcr.Text = r;
            //
            btOk_Click(null, null);
        }

        #region 画矩形
        private DrawList m_drawList;
        private DrawBase m_curDraw;
        private Rectangle m_curBound;
        private Rectangle m_lastBound;
        private Point m_StartPoint;

        /// <summary>
        /// 构造对象
        /// </summary>
        private void InitObjs()
        {
            this.m_drawList = new DrawList(this);

            DrawBase.HalfGrab = 5;
            LineObj line0 = new LineObj(new Point(10, 10), new Point(100, 100));
            line0.ForeColor = Color.Red;

            LineObj line1 = new LineObj(new Point(16, 67), new Point(100, 180));
            line1.ForeColor = Color.Blue;

            RectangleObj rect = new RectangleObj(new Point(120, 70), new Point(220, 200));
            rect.ForeColor = Color.Green;

            ButtonObj button = new ButtonObj(new Point(20, 20), new Point(90, 45));
            button.ForeColor = Color.Black;
            button.Text = "button1";
            //m_drawList.Add(line0);
            //m_drawList.Add(line1);
            //m_drawList.Add(rect);
            //m_drawList.Add(button);
        }
        /// <summary>
        /// 绘制对象
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            this.m_drawList.Draw(e.Graphics);
            base.OnPaint(e);
        }
        /// <summary>
        /// 重写以移动对象或设置鼠标
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (this.Capture)
            {
                if (this.m_curDraw != null)
                {
                    this.moveObj(e);
                }
                return;
            }
            if (this.m_drawList.GetNear(e.X, e.Y) != null)
            {
                this.Cursor = Cursors.SizeAll;
            }
            else
            {
                this.Cursor = Cursors.Default;
            }
        }
        /// <summary>
        /// 重写以获取鼠标下的对象
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            this.getObj(e);
        }
        /// <summary>
        /// 重写以按Delete键删除当前对象
        /// </summary>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Delete)
            {
                if (this.m_curDraw != null)
                {
                    this.deleteObj();
                }
            }
            return base.ProcessDialogKey(keyData);
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.m_curDraw != null)
            {
                this.deleteObj();
            }
            else
            {
                MessageBox.Show(this, "没有选中的对象，请使用鼠标点选择一个！", "提示");
            }
        }
        private void moveObj(MouseEventArgs e)
        {
            Rectangle bound = this.m_curBound;

            bound.Offset(e.X - this.m_StartPoint.X, e.Y - this.m_StartPoint.Y);
            this.m_curDraw.SetBound(bound);

            Rectangle _last = this.m_lastBound;
            Rectangle _bound = bound;
            _last.Inflate(2, 2);
            _bound.Inflate(2, 2);

            using (Region invReg = new Region(_last))
            {
                invReg.Union(_bound);
                this.Invalidate(invReg);
            }
            this.m_lastBound = bound;
        }
        private void getObj(MouseEventArgs e)
        {
            this.m_curDraw = this.m_drawList.GetNear(e.X, e.Y);
            if (this.m_curDraw != null)
            {
                this.m_curBound = this.m_curDraw.GetBound();
                this.m_StartPoint = e.Location;
            }
        }
        private void deleteObj()
        {
            Rectangle bound = this.m_curDraw.GetBound();
            this.m_drawList.Remove(this.m_curDraw);
            this.m_curDraw = null;
            bound.Inflate(2, 2);
            this.Invalidate(bound);
        }


        private void frmMain_MouseDown(object sender, MouseEventArgs e)
        {
        }
        private void frmMain_MouseUp(object sender, MouseEventArgs e)
        {
        }
        private void frmMain_MouseMove(object sender, MouseEventArgs e)
        {
        }
        private void frmMain_Paint(object sender, PaintEventArgs e)
        {
        }
        #endregion

        #region panel拖动 调整
        private enum EnumMousePointPosition
        {
            MouseSizeNone = 0, //'无  
            MouseSizeRight = 1, //'拉伸右边框  
            MouseSizeLeft = 2, //'拉伸左边框  
            MouseSizeBottom = 3, //'拉伸下边框  
            MouseSizeTop = 4, //'拉伸上边框  
            MouseSizeTopLeft = 5, //'拉伸左上角  
            MouseSizeTopRight = 6, //'拉伸右上角  
            MouseSizeBottomLeft = 7, //'拉伸左下角  
            MouseSizeBottomRight = 8, //'拉伸右下角  
            MouseDrag = 9  // '鼠标拖动  
        }
        const int Band = 5;
        const int MinWidth = 10;
        const int MinHeight = 10;
        private EnumMousePointPosition m_MousePointPosition;
        private Point p, p1;
        private void MyMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            p.X = e.X;
            p.Y = e.Y;
            p1.X = e.X;
            p1.Y = e.Y;
        }
        private void MyMouseLeave(object sender, System.EventArgs e)
        {
            m_MousePointPosition = EnumMousePointPosition.MouseSizeNone;
            this.Cursor = Cursors.Arrow;
        }
        private EnumMousePointPosition MousePointPosition(Size size, System.Windows.Forms.MouseEventArgs e)
        {

            if ((e.X >= -1 * Band) | (e.X <= size.Width) | (e.Y >= -1 * Band) | (e.Y <= size.Height))
            {
                if (e.X < Band)
                {
                    if (e.Y < Band) { return EnumMousePointPosition.MouseSizeTopLeft; }
                    else
                    {
                        if (e.Y > -1 * Band + size.Height)
                        { return EnumMousePointPosition.MouseSizeBottomLeft; }
                        else
                        { return EnumMousePointPosition.MouseSizeLeft; }
                    }
                }
                else
                {
                    if (e.X > -1 * Band + size.Width)
                    {
                        if (e.Y < Band)
                        { return EnumMousePointPosition.MouseSizeTopRight; }
                        else
                        {
                            if (e.Y > -1 * Band + size.Height)
                            { return EnumMousePointPosition.MouseSizeBottomRight; }
                            else
                            { return EnumMousePointPosition.MouseSizeRight; }
                        }
                    }
                    else
                    {
                        if (e.Y < Band)
                        { return EnumMousePointPosition.MouseSizeTop; }
                        else
                        {
                            if (e.Y > -1 * Band + size.Height)
                            { return EnumMousePointPosition.MouseSizeBottom; }
                            else
                            { return EnumMousePointPosition.MouseDrag; }
                        }
                    }
                }
            }
            else
            { return EnumMousePointPosition.MouseSizeNone; }
        }
        private void MyMouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            Control lCtrl = (sender as Control);

            if (e.Button == MouseButtons.Left)
            {
                switch (m_MousePointPosition)
                {
                    case EnumMousePointPosition.MouseDrag:
                        lCtrl.Left = lCtrl.Left + e.X - p.X;
                        lCtrl.Top = lCtrl.Top + e.Y - p.Y;
                        break;
                    case EnumMousePointPosition.MouseSizeBottom:
                        lCtrl.Height = lCtrl.Height + e.Y - p1.Y;
                        p1.X = e.X;
                        p1.Y = e.Y; //'记录光标拖动的当前点  
                        break;
                    case EnumMousePointPosition.MouseSizeBottomRight:
                        lCtrl.Width = lCtrl.Width + e.X - p1.X;
                        lCtrl.Height = lCtrl.Height + e.Y - p1.Y;
                        p1.X = e.X;
                        p1.Y = e.Y; //'记录光标拖动的当前点  
                        break;
                    case EnumMousePointPosition.MouseSizeRight:
                        lCtrl.Width = lCtrl.Width + e.X - p1.X;
                        //    lCtrl.Height = lCtrl.Height + e.Y - p1.Y;  
                        p1.X = e.X;
                        p1.Y = e.Y; //'记录光标拖动的当前点  
                        break;
                    case EnumMousePointPosition.MouseSizeTop:
                        lCtrl.Top = lCtrl.Top + (e.Y - p.Y);
                        lCtrl.Height = lCtrl.Height - (e.Y - p.Y);
                        break;
                    case EnumMousePointPosition.MouseSizeLeft:
                        lCtrl.Left = lCtrl.Left + e.X - p.X;
                        lCtrl.Width = lCtrl.Width - (e.X - p.X);
                        break;
                    case EnumMousePointPosition.MouseSizeBottomLeft:
                        lCtrl.Left = lCtrl.Left + e.X - p.X;
                        lCtrl.Width = lCtrl.Width - (e.X - p.X);
                        lCtrl.Height = lCtrl.Height + e.Y - p1.Y;
                        p1.X = e.X;
                        p1.Y = e.Y; //'记录光标拖动的当前点  
                        break;
                    case EnumMousePointPosition.MouseSizeTopRight:
                        lCtrl.Top = lCtrl.Top + (e.Y - p.Y);
                        lCtrl.Width = lCtrl.Width + (e.X - p1.X);
                        lCtrl.Height = lCtrl.Height - (e.Y - p.Y);
                        p1.X = e.X;
                        p1.Y = e.Y; //'记录光标拖动的当前点  
                        break;
                    case EnumMousePointPosition.MouseSizeTopLeft:
                        lCtrl.Left = lCtrl.Left + e.X - p.X;
                        lCtrl.Top = lCtrl.Top + (e.Y - p.Y);
                        lCtrl.Width = lCtrl.Width - (e.X - p.X);
                        lCtrl.Height = lCtrl.Height - (e.Y - p.Y);
                        break;
                    default:
                        break;
                }
                if (lCtrl.Width < MinWidth) lCtrl.Width = MinWidth;
                if (lCtrl.Height < MinHeight) lCtrl.Height = MinHeight;

            }
            else
            {
                m_MousePointPosition = MousePointPosition(lCtrl.Size, e);  //'判断光标的位置状态  
                switch (m_MousePointPosition)  //'改变光标  
                {
                    case EnumMousePointPosition.MouseSizeNone:
                        this.Cursor = Cursors.Arrow;    //'箭头  
                        break;
                    case EnumMousePointPosition.MouseDrag:
                        this.Cursor = Cursors.SizeAll;   //'四方向  
                        break;
                    case EnumMousePointPosition.MouseSizeBottom:
                        this.Cursor = Cursors.SizeNS;    //'南北  
                        break;
                    case EnumMousePointPosition.MouseSizeTop:
                        this.Cursor = Cursors.SizeNS;    //'南北  
                        break;
                    case EnumMousePointPosition.MouseSizeLeft:
                        this.Cursor = Cursors.SizeWE;    //'东西  
                        break;
                    case EnumMousePointPosition.MouseSizeRight:
                        this.Cursor = Cursors.SizeWE;    //'东西  
                        break;
                    case EnumMousePointPosition.MouseSizeBottomLeft:
                        this.Cursor = Cursors.SizeNESW;   //'东北到南西  
                        break;
                    case EnumMousePointPosition.MouseSizeBottomRight:
                        this.Cursor = Cursors.SizeNWSE;   //'东南到西北  
                        break;
                    case EnumMousePointPosition.MouseSizeTopLeft:
                        this.Cursor = Cursors.SizeNWSE;   //'东南到西北  
                        break;
                    case EnumMousePointPosition.MouseSizeTopRight:
                        this.Cursor = Cursors.SizeNESW;   //'东北到南西  
                        break;
                    default:
                        break;
                }
            }

        } 
        #endregion

        private void pCutSet_SizeChanged(object sender, EventArgs e)
        {
            pCut.Size = pCutSet.Size;
        }

        private void btOk_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(edtOcr.Text))
                return;
            webBrowser1.Url = new Uri(string.Format(gsSearchUrl,edtOcr.Text));
            //webBrowser1.Navigate(new Uri(url));
            ////下面的是判断页面是否加载完成，如果未完成则继续加载。
            //while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
            //{
            //    System.Windows.Forms.Application.DoEvents();
            //}
        }

        private void toolStripMenuItem_Click(object sender, EventArgs e)
        {
            string sTag = ((ToolStripMenuItem)sender).Tag.ToString();
            for (int i = 0; i < 搜索页面ToolStripMenuItem.DropDownItems.Count; i++)
            {
                搜索页面ToolStripMenuItem.DropDownItems[i].Text = 搜索页面ToolStripMenuItem.DropDownItems[i].Tag.ToString();
            }
            switch (sTag)
            {
                case "百度(电脑版)":
                    ((ToolStripMenuItem)sender).Text = sTag + " √";
                    gsSearchUrl = "https://www.baidu.com/s?wd={0}";
                    break;
                case "百度(手机版)":
                    ((ToolStripMenuItem)sender).Text = sTag + " √";
                    gsSearchUrl = "https://m.baidu.com/s?word={0}";
                    break;
                case "Google":
                    ((ToolStripMenuItem)sender).Text = sTag + " √";
                    gsSearchUrl = "https://www.google.com/search?q={0}";
                    break;
                case "360":
                    ((ToolStripMenuItem)sender).Text = sTag + " √";
                    gsSearchUrl = "https://www.so.com/s?q={0}";
                    break;
                default:
                    break;
            }
        }
        private void 隐藏截图预览ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (隐藏截图预览ToolStripMenuItem.Text.Contains("√"))
            {
                隐藏截图预览ToolStripMenuItem.Text = "隐藏截图预览";
                pCut.Visible = true;
            }
            else
            {
                隐藏截图预览ToolStripMenuItem.Text = "隐藏截图预览 √";
                pCut.Visible = false;
            }
        }

        #region  webBrower
        private void InjectAlertBlocker()
        {//ebBrowser控件禁用alert,confirm之类的弹窗
            HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
            HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
            mshtml.IHTMLScriptElement element = (mshtml.IHTMLScriptElement)scriptEl.DomElement;
            string alertBlocker = "window.alert = function () { }";
            element.text = alertBlocker;
            head.AppendChild(scriptEl);
            webBrowser1.ScriptErrorsSuppressed = true;
        }
        private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
        {//阻止弹出新窗口
            e.Cancel = true;
        }
        private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {//必须要在这个事件里面调用才能禁用，之前一直写在别的事件里面，浪费了2小时。
            InjectAlertBlocker();
        }
        #endregion

        private void edtOcr_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter)
                return;

            btOk_Click(null, null);
        }

        private void btOcr_Click(object sender, EventArgs e)
        {
            string r = GeneralEnhanced();
            edtOcr.Text = r;
        }




    }

    #region 画矩形
    abstract class DrawBase//定义图像的基类
    {
        internal Color m_BackColor;
        internal Color m_ForeColor;
        internal static int m_HalfGrab;

        public static int HalfGrab
        {
            get { return DrawBase.m_HalfGrab; }
            set { DrawBase.m_HalfGrab = value; }
        }

        public Color BackColor
        {
            get { return m_BackColor; }
            set { m_BackColor = value; }
        }

        public Color ForeColor
        {
            get { return m_ForeColor; }
            set { m_ForeColor = value; }
        }

        public abstract Rectangle GetBound();
        public abstract void Draw(Graphics g);
        public abstract bool Near(int x, int y);
        public abstract void SetBound(Rectangle bound);
    }
    class RectangleObj : DrawBase//定义矩形
    {
        private Point m_Start;
        private Point m_End;
        private bool m_Solid;
        public RectangleObj(Point start, Point end)
        {
            this.m_Start = start;
            this.m_End = end;
        }
        public bool Solid
        {
            get { return m_Solid; }
            set { m_Solid = value; }
        }
        public override System.Drawing.Rectangle GetBound()
        {
            int x = this.m_Start.X < this.m_End.X ? this.m_Start.X : this.m_End.X;
            int y = this.m_Start.Y < this.m_End.Y ? this.m_Start.Y : this.m_End.Y;
            int r = this.m_Start.X < this.m_End.X ? this.m_End.X : this.m_Start.X;
            int b = this.m_Start.Y < this.m_End.Y ? this.m_End.Y : this.m_Start.Y;
            return Rectangle.FromLTRB(x, y, r, b);
    }

        public override void Draw(Graphics g)
        {
            Rectangle bound = this.GetBound();
            if (this.m_Solid)
            {
                using (SolidBrush brush = new SolidBrush(this.m_BackColor))
                {
                    g.FillRectangle(brush, bound);
                }
            }
            using (Pen pen = new Pen(this.m_ForeColor,4))
            {
                g.DrawRectangle(pen, bound);
            }
        }

        public override bool Near(int x, int y)
        {
            Rectangle bound = this.GetBound();
            Rectangle inner = bound;
            Rectangle outer = bound;
            inner.Inflate(-m_HalfGrab, -m_HalfGrab);
            outer.Inflate(m_HalfGrab, m_HalfGrab);
            Region reg = new Region(outer);
            reg.Exclude(inner);
            return reg.IsVisible(x, y);
        }

        public override void SetBound(Rectangle bound)
        {
            this.m_Start = new Point(bound.X, bound.Y);
            this.m_End = new Point(bound.Right, bound.Bottom);
        }
    }
    class DrawList : List<DrawBase>//定义图像的集合对象
    {
        private Control m_Owner;
        public DrawList(Control owner)
        {
            this.m_Owner = owner;
        }
        internal DrawBase GetNear(int x, int y)
        {
            foreach (DrawBase draw in this)
            {
                if (draw.Near(x, y))
                {
                    return draw;
                }
            }
            return null;
        }

        internal void Draw(Graphics graphics)
        {
            foreach (DrawBase draw in this)
            {
                draw.Draw(graphics);
            }
        }
    }    
    class LineObj : DrawBase//定义线
    {
        private Point m_Start;
        private Point m_End;
        public LineObj(Point start, Point end)
        {
            this.m_Start = start;
            this.m_End = end;
        }
        public override System.Drawing.Rectangle GetBound()
        {
            int x = this.m_Start.X < this.m_End.X ? this.m_Start.X : this.m_End.X;
            int y = this.m_Start.Y < this.m_End.Y ? this.m_Start.Y : this.m_End.Y;
            int r = this.m_Start.X < this.m_End.X ? this.m_End.X : this.m_Start.X;
            int b = this.m_Start.Y < this.m_End.Y ? this.m_End.Y : this.m_Start.Y;
            return Rectangle.FromLTRB(x, y, r, b);
        }
        public override void Draw(System.Drawing.Graphics g)
        {
            using (Pen pen = new Pen(this.m_ForeColor,2))
            {
                g.DrawLine(pen, this.m_Start, this.m_End);
            }
        }
        public override bool Near(int x, int y)
        {
            //点到直线的距离是否在抓取范围之内
            float A = this.m_End.Y - this.m_Start.Y;
            float B = this.m_End.X - this.m_Start.X;
            float C = B * this.m_Start.Y - A * this.m_Start.X;
            double D = (A * x - B * y + C) / (Math.Sqrt(A * A + B * B));
            if (D >= -m_HalfGrab && D <= m_HalfGrab)
            {
                RectangleF bounds = this.GetBound();
                bounds.Inflate(m_HalfGrab, m_HalfGrab);
                return bounds.Contains(x, y);
            }
            return false;
        }
        public override void SetBound(Rectangle bound)
        {
            this.m_Start = new Point(bound.X, bound.Y);
            this.m_End = new Point(bound.Right, bound.Bottom);
        }
    }
    class ButtonObj : DrawBase//按钮定义
    {
        private Point m_Start;
        private Point m_End;
        private string m_Text;
        public ButtonObj(Point start, Point end)
        {
            this.m_Start = start;
            this.m_End = end;
        }
        public string Text
        {
            get { return m_Text; }
            set { m_Text = value; }
        }
        public override System.Drawing.Rectangle GetBound()
        {
            int x = this.m_Start.X < this.m_End.X ? this.m_Start.X : this.m_End.X;
            int y = this.m_Start.Y < this.m_End.Y ? this.m_Start.Y : this.m_End.Y;
            int r = this.m_Start.X < this.m_End.X ? this.m_End.X : this.m_Start.X;
            int b = this.m_Start.Y < this.m_End.Y ? this.m_End.Y : this.m_Start.Y;
            return Rectangle.FromLTRB(x, y, r, b);
        }
        public override void Draw(Graphics g)
        {
            Rectangle bound = this.GetBound();
            using (Pen pen = new Pen(this.m_ForeColor))
            {
                ControlPaint.DrawButton(g, bound, ButtonState.Normal);
                using (SolidBrush brush = new SolidBrush(this.m_ForeColor))
                {
                    using (Font font = new Font("宋体", 10))
                    {
                        using (StringFormat format = new StringFormat())
                        {
                            format.Alignment = StringAlignment.Center;
                            format.LineAlignment = StringAlignment.Center;
                            g.DrawString(this.m_Text, font, brush, bound, format);
                        }
                    }
                }
            }
        }
        public override bool Near(int x, int y)
        {
            Rectangle bound = this.GetBound();
            Rectangle outer = bound;
            outer.Inflate(m_HalfGrab, m_HalfGrab);
            return outer.Contains(x, y);
        }
        public override void SetBound(Rectangle bound)
        {
            this.m_Start = new Point(bound.X, bound.Y);
            this.m_End = new Point(bound.Right, bound.Bottom);
        }
    }
    #endregion
    #region 百度文字OCR
    public class baiduOcr
    {
        public string log_id;
        public string words_result_num;
        public words_result[] words_result;
    }
    public class words_result
    {
        public string words;
    }
    #endregion

}
